package service.clazz.strategy;

import entity.table.TableDto;
import entity.table.TableFileDto;
import entity.table.TableInfoDto;
import enums.table.ClazzTypeEnum;
import enums.table.InitImportEnum;
import service.clazz.interfaces.ClazzStrategy;

import java.util.List;

/**
 * 实体类
 */
public class ClazzServiceControllerStrategy extends ClazzStrategy {

    @Override
    public TableFileDto enerateStrategy(List<TableDto> list, TableInfoDto table) {
        TableFileDto tableFileDto = new TableFileDto();
        tableFileDto.setFileName(table.getTableNameCamel(ClazzTypeEnum.SERVICECONTROLLER)+".java");
        tableFileDto.setContext(this.bringClazz(list, table));
        return tableFileDto;
    }

    /**
     * 生成类
     */
    public String bringClazz(List<TableDto> list, TableInfoDto table) {
        StringBuilder str = new StringBuilder();


        // 类路径
        str.append(super.bringPackage(table.getServicePackaging()));
        // 类引用
        str.append(super.bringImport(this.importList(table))).append("\n");
        // 类内容
        str.append("// TODO 修改 value\n");
        str.append("@RestController\n");
        str.append("@RequestMapping(value = \"/xxx/xxx\")");
        str.append(super.bringBody(ClazzTypeEnum.SERVICECONTROLLER, table.getTableNameCamel(), this.body(table)));
        // 输出
        return str.toString();
    }

    private List<String> importList(TableInfoDto table) {
        List<String> list = InitImportEnum.initEntityList();
        list.add(InitImportEnum.Page.getClazzImport());
        list.add(InitImportEnum.Autowired.getClazzImport());
        list.add(InitImportEnum.PageResult.getClazzImport());
        list.add(InitImportEnum.RequestMapping.getClazzImport());
        list.add(InitImportEnum.RequestParam.getClazzImport());
        list.add(InitImportEnum.RestController.getClazzImport());
        list.add(InitImportEnum.RequestBody.getClazzImport());
        list.add(InitImportEnum.PostMapping.getClazzImport());
        list.add(table.getEntityPackaging() + ".*");
        list.add(table.getServiceImplPackaging() + "." + table.getTableNameCamel(ClazzTypeEnum.SERVICEIMPL));
        return list;
    }

    private String body(TableInfoDto table) {
        return super.codeTemple("ServiceController", table);
    }
}
